package com.pwc.conversionTool.controller;

import static com.pwc.conversionTool.controller.sessionPersistence.AccountSessionPersistence.getCurrentAccount;
import static com.pwc.conversionTool.controller.sessionPersistence.AccountSessionPersistence.isLogin;

import java.util.Arrays;
import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.pwc.conversionTool.biz.SoapConnectionBiz;
import com.pwc.conversionTool.entity.SoapConnection;



@Controller
public class SoapController {

	private static final Logger logger = LoggerFactory.getLogger(SoapController.class);
	
	private SoapConnectionBiz soapConnectionbiz = new SoapConnectionBiz();

	@Autowired
	public ServletContext context;

	/**
	 * give a page for test soap connection
	 * 
	 * @param locale
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/soap/testSoap", method = RequestMethod.GET)
	public String testSoap(Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}
		return "soap/testSoap";
	}

	
	
	/**
	 * when start to create a connection
	 * @param locale
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/soap/createConnection", method = RequestMethod.GET)
	public String createSoapConnection(Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}
		
		model.addAttribute("isEdit", true);
		
		return "soap/createConnection";
	}
	
	@RequestMapping(value = "/soap/saveConnection", method = RequestMethod.POST)
	public String saveSoapConnection(SoapConnection soapConnection, Locale locale, Model model, HttpSession session) {	
		
		try {
			soapConnectionbiz.saveSoapConnection(soapConnection,getCurrentAccount(session));			
			model.addAttribute("isEdit", false);
			model.addAttribute("soapConnection", soapConnection);
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}
		return "soap/createConnection";
	}
	
	
	
	
	@RequestMapping(value = "/soap/viewDetail", method = RequestMethod.POST)
	public String viewDetail(Long id, Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}
		
		try {
			SoapConnection soapConnection= soapConnectionbiz.getSoapConnectionById(id);
			model.addAttribute("soapConnection", soapConnection);
			model.addAttribute("isEdit", false);
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));			
		}
		
		return "soap/createConnection";
	}
	
	

	@RequestMapping(value = "/soap/manageConnection", method = RequestMethod.GET)
	public String manageConnection(Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}
		return "soap/manageConnection";
	}

}
